package view;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import service.UserService;
import util.BaseFrame;
import util.MySpring;

public class RegisterFrame extends BaseFrame{
	
	public RegisterFrame() {
		this.init();
	}
	public RegisterFrame(String title) {
		super(title);
		this.init();
	}
	
	private	JPanel mainPanel = new JPanel();
	private	JLabel titleLabel = new JLabel("用户注册");
	private	JLabel accountLabel = new JLabel("账 号：");
	private	JLabel passwordLabel = new JLabel("密 码：");
	private	JLabel conPasswordLabel = new JLabel("确认密码：");
	private	JTextField accountField = new JTextField();
	private	JPasswordField passwordField = new JPasswordField();
	private	JPasswordField conPasswordField = new JPasswordField();
	private	JButton finishButton = new JButton("完成注册");
	private	JButton returnButton = new JButton("返回");
	
	@Override
	protected void setFontAndSoOn() {
		mainPanel.setLayout(null);
		titleLabel.setBounds(120, 40, 345, 35);
		titleLabel.setFont(new Font("微软雅黑",Font.BOLD,34));
		accountLabel.setBounds(19, 124, 90, 30);
		accountLabel.setFont(new Font("微软雅黑",Font.PLAIN,24));
		accountField.setBounds(139, 124, 200, 30);
		accountField.setFont(new Font("微软雅黑",Font.BOLD,24));
		passwordLabel.setBounds(19, 174, 90, 30);
		passwordLabel.setFont(new Font("微软雅黑",Font.PLAIN,24));
		passwordField.setBounds(139, 174, 200, 30);
		passwordField.setFont(new Font("",Font.BOLD,24));
		conPasswordLabel.setBounds(19, 224, 200, 30);
		conPasswordLabel.setFont(new Font("微软雅黑",Font.PLAIN,24));
		conPasswordField.setBounds(139, 224, 200, 30);
		conPasswordField.setFont(new Font("",Font.BOLD,24));
		finishButton.setBounds(40,300,130,30);
		finishButton.setFont(new Font("微软雅黑",Font.PLAIN,20));
		returnButton.setBounds(230,300,100,30);
		returnButton.setFont(new Font("微软雅黑",Font.PLAIN,20));
	}

	@Override
	protected void addElement() {
		mainPanel.add(titleLabel);
		mainPanel.add(accountLabel);
		mainPanel.add(accountField);
		mainPanel.add(passwordLabel);
		mainPanel.add(passwordField);
		mainPanel.add(conPasswordLabel);
		mainPanel.add(conPasswordField);
		mainPanel.add(finishButton);
		mainPanel.add(returnButton);
		this.add(mainPanel);
	}

	@Override
	protected void addListener() {
		ActionListener finishListener = new ActionListener() {	
			@Override
			public void actionPerformed(ActionEvent e) {
				String account = accountField.getText();
				String password = passwordField.getText();
				String conPassword = conPasswordField.getText();
				if(account.equals("")||password.equals("")||conPassword.equals("")) {
					JOptionPane.showMessageDialog(RegisterFrame.this, "不能为空");
					return;
				}
				if(password.equals(conPassword)) {
					UserService service = MySpring.getBean("service.UserService");
					String result = service.register(account, password);
					if(result.equals("注册成功")) {
						RegisterFrame.this.setVisible(false);
						new LoginFrame("登录窗口");
					}else {
						JOptionPane.showMessageDialog(RegisterFrame.this, result);
						RegisterFrame.this.setNull();
					}
				}else {
					JOptionPane.showMessageDialog(RegisterFrame.this, "密码不一致");
					RegisterFrame.this.setNull();
				}
			}
			
		};
		finishButton.addActionListener(finishListener);
		
		ActionListener returnListener = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				
				RegisterFrame.this.setVisible(false);
				new LoginFrame("登录窗口");
			}
		};
		returnButton.addActionListener(returnListener);
	}

	@Override
	protected void setFrameSelf() {
		this.setCenterFrame(400, 450);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);
		this.setVisible(true);
	}
	//文本置空
	private void setNull() {
		accountField.setText("");
		passwordField.setText("");
		conPasswordField.setText("");
	}

}
